package com.xbongbong.paas.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.enums.ComboTypeEnum;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.pojo.FieldAttrEntityForImport;
import com.xbongbong.paas.field.ItemPoJo;
import com.xbongbong.paas.field.MultiUnitItemPoJo;
import com.xbongbong.paas.field.SubFormPoJo;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.CollectionsUtil;
import com.xbongbong.paas.toolbox.util.RegexUtil;
import com.xbongbong.paas.toolbox.util.StringUtil;
import com.xbongbong.pro.enums.ErrorCodeEnum;
import com.xbongbong.saas.constant.BasicConstant;
import com.xbongbong.pro.enums.errorcodes.BusinessRuleErrorCodeEnum;
import com.xbongbong.saas.constant.StringConstant;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.atomic.AtomicReference;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 下拉框的一些回显工具类
 * @author 吴峰
 * @version v1.0
 * @date 2019/5/28 18:34
 * @since v1.0
 */
public class ItemUtil {

    private static final Logger LOG = LoggerFactory.getLogger(ItemUtil.class);

    private static final String SEPARATOR = "、";

    /**
     * 含字母
     */
    private static final Pattern isLetter = Pattern.compile(".*[a-zA-Z]+.*");
    /**
     * 含中文
     */
    private static final Pattern isChinese = Pattern.compile("[\\u4E00-\\u9FA5\\uF900-\\uFA2D]");

    /**
     * 含横杠
     */
    private static final Pattern isMinus = Pattern.compile(".*\\-+.*");

    private static final  Pattern IS_UUID = Pattern.compile("^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$");

    /**
     * 获取下拉框的数据，包括自定义和数据联动等
     * @param fieldAttrEntity
     * @param jsonObject
     * @return
     */
    public static Object getComBoValue(FieldAttrEntity fieldAttrEntity, JSONObject jsonObject ) {
        String attr = fieldAttrEntity.getAttr();
        if (Objects.equals(fieldAttrEntity.getComboType(), ComboTypeEnum.CUSTOM.getType())) {
            Object itemValue = ItemUtil.getCustomItemList(fieldAttrEntity, jsonObject);
            return itemValue;
        } else if (Objects.equals(fieldAttrEntity.getComboType(), ComboTypeEnum.DATARELY.getType()) ||
                Objects.equals(fieldAttrEntity.getComboType(), ComboTypeEnum.LINKFORM.getType())||
                Objects.equals(fieldAttrEntity.getComboType(), ComboTypeEnum.BUSINESS.getType())) {
            List<ItemPoJo> itemPoJos = fieldAttrEntity.getItems();
            Map<Object, ItemPoJo> itemMap = new HashMap<>(itemPoJos.size());
            Set<String> textSet = new HashSet<>();
            itemPoJos.forEach(item->{
                itemMap.put(item.getValue(), item);
                textSet.add(item.getText());
            });
            if (Objects.nonNull(jsonObject.get(attr))) {
                if (jsonObject.get(attr) instanceof List) {
                    JSONArray jsonArray = jsonObject.getJSONArray(attr);
                    for (Object object: jsonArray) {
                        String value = object.toString();
                        if (itemMap.containsKey(value)) {
                            Object itemValue = ItemUtil.getCustomItemList(fieldAttrEntity, jsonObject);
                            return itemValue;
                        } else if (ItemUtil.isUUID(value) && !textSet.contains(value)) {
                            // 是uuid并且文本中没有这种数据
                            return null;
                        } else {
                           return jsonObject.get(attr);
                        }
                    }
                }else if (itemMap.containsKey(jsonObject.get(attr))) {
                    Object itemValue = ItemUtil.getCustomItemList(fieldAttrEntity, jsonObject);
                    return itemValue;
                } else {
                    return jsonObject.get(attr);
                }
            }
        }
        return jsonObject.get(attr);
    }

    /**
     * 获取自定义的下拉框
     * @param fieldAttrEntity
     * @param jsonObject
     * @return 下拉框值对应的显示
     */
    public static Object getCustomItemList(FieldAttrEntity fieldAttrEntity, JSONObject jsonObject) {
        List<ItemPoJo> itemPoJos = fieldAttrEntity.getItems();
        Map<String, ItemPoJo> itemMap = new HashMap<>(itemPoJos.size());
        Set<String> textSet = new HashSet<>();
        itemPoJos.forEach(item->{
            if (Objects.nonNull(item.getValue())) {
                itemMap.put(item.getValue().toString(), item);
                textSet.add(item.getText());
            }
        });
        String key = fieldAttrEntity.getAttr();
        Object itemValue = jsonObject.get(key);
        if (itemValue instanceof ArrayList || itemValue instanceof JSONArray) {
            JSONArray list = jsonObject.getJSONArray(key);
            if (Objects.isNull(list)) {
                list = new JSONArray();
            }
            if (!list.isEmpty()) {
                List<Object> valueList = new ArrayList<>();
                for(Object value: list) {
                    if (Objects.isNull(value)) {
                        continue;
                    }
                    ItemPoJo itemPoJo = itemMap.get(value.toString());
                    if (Objects.nonNull(itemPoJo)) {
                        valueList.add(itemPoJo.getText());
                    } else if (Objects.nonNull(value) && (!isUUID(value.toString()) || textSet.contains(value))) {
                        // 如果是uuId，则应该是已经被删了的下拉框
                        valueList.add(value);
                    }
                }
                itemValue = valueList;
            }
        } else if (Objects.nonNull(jsonObject.get(key))) {
            Object value = jsonObject.get(key);
            if (StringUtil.isEmpty(value.toString())){
                return "";
            }
            ItemPoJo itemPoJo = itemMap.get(value.toString());
            if (Objects.nonNull(itemPoJo)) {
                itemValue = itemPoJo.getText();
            }   else if (textSet.contains(value)) {
               return value;
            }  else if (isDeleteCheck(value, fieldAttrEntity)) {
                // 如果是uuId，则应该是已经被删了的下拉框
                itemValue = "";
            } else {
                itemValue = value.toString();
            }
        }
        return itemValue;
    }

    /**
     * 获取通用的选项
     *
     * @param fieldAttrEntity
     * @param jsonObject
     * @return
     */
    public static Object getCommonItem(FieldAttrEntity fieldAttrEntity, JSONObject jsonObject) {
        Object value = jsonObject.get(fieldAttrEntity.getAttr());
        if (value instanceof ArrayList || value instanceof JSONArray){
            return getCommonMultiItem(fieldAttrEntity, jsonObject);
        }else {
            return getCommonSimpleItem(fieldAttrEntity, jsonObject);
        }
    }

    /**
     * 获取通用的单选、下拉单选选项
     *
     * @param fieldAttrEntity
     * @param jsonObject
     * @return 单个数据选项
     */
    public static ItemPoJo getCommonSimpleItem(FieldAttrEntity fieldAttrEntity, JSONObject jsonObject) {
        //拿到选项值
        String key = fieldAttrEntity.getAttr();
        Object value = jsonObject.get(key);
        if (Objects.isNull(value) || StringUtil.isEmpty(value.toString())) {
            return null;
        }
        //移动端会出现解析两次的情况
        if (value instanceof ItemPoJo){
            return (ItemPoJo) value;
        }
        String valueStr = value.toString();
        //获取到所有解释项放入Map和Set
        List<ItemPoJo> itemPoJos = fieldAttrEntity.getItems();
        Map<String, ItemPoJo> itemMap = new HashMap<>(itemPoJos.size());
        AtomicReference<String> otherColor = new AtomicReference<>("");
        itemPoJos.forEach(item -> {
            if (Objects.nonNull(item.getValue())) {
                itemMap.put(item.getValue().toString(), item);
            }
            if (Objects.equals(item.getIsOther(), BasicConstant.ONE)){
                otherColor.set(item.getColor());
            }
        });

        ItemPoJo itemPoJo = itemMap.get(valueStr);
        ItemPoJo simpleItem;
        if (Objects.nonNull(itemPoJo)) {
            // 自定义选项的值
            simpleItem = new ItemPoJo(itemPoJo.getText(), itemPoJo.getValue(), itemPoJo.getChecked(), itemPoJo.getColor());
        } else if (isDeleteCheck(value, fieldAttrEntity)) {
            simpleItem = null;
        } else {
            // 1. 一开始设置的是数据联动或者联动其他表单，此时无颜色；
            // 2. 又改为自定义选项，并且加了其他项，那么原来的数据联动和联动其他表单带过来的值，会默认带上其他项的颜色
            simpleItem = new ItemPoJo(valueStr, valueStr, false, otherColor.get());
        }
        return simpleItem;
    }

    /**
     * 获取通用的复选、下拉复选选项
     *
     * @param fieldAttrEntity
     * @param jsonObject
     * @return 数据选项列表
     */
    public static List<ItemPoJo> getCommonMultiItem(FieldAttrEntity fieldAttrEntity, JSONObject jsonObject) {
        //拿到选项值
        String key = fieldAttrEntity.getAttr();
        List list = (List) jsonObject.get(key);
        if (Objects.isNull(list) || list.isEmpty()) {
            return null;
        }
        //获取到所有选项放入Map和Set
        List<ItemPoJo> itemPoJos = fieldAttrEntity.getItems();
        Map<String, ItemPoJo> itemMap = new HashMap<>(itemPoJos.size());
        Set<String> textSet = new HashSet<>();
        AtomicReference<String> otherColor = new AtomicReference<>("");
        itemPoJos.forEach(item -> {
            if (Objects.nonNull(item.getValue())) {
                itemMap.put(item.getValue().toString(), item);
                textSet.add(item.getText());
            }
            if (Objects.equals(item.getIsOther(), BasicConstant.ONE)){
                otherColor.set(item.getColor());
            }
        });
        List<ItemPoJo> multiItem = new ArrayList<>();
        for (Object value : list) {
            if (Objects.isNull(value)) {
                continue;
            }
            if (value instanceof ItemPoJo){
                multiItem.add((ItemPoJo) value);
                continue;
            }
            String valueStr = value.toString();
            if (StringUtil.isEmpty(valueStr)){
                continue;
            }
            ItemPoJo itemPoJo = itemMap.get(valueStr);
            if (Objects.nonNull(itemPoJo)) {
                multiItem.add(new ItemPoJo(itemPoJo.getText(), itemPoJo.getValue(), itemPoJo.getChecked(), itemPoJo.getColor()));
            } else if (!isUUID(valueStr) || textSet.contains(valueStr)) {
                multiItem.add(new ItemPoJo(valueStr, valueStr, false, otherColor.get()));
            }
        }
        if (CollectionsUtil.isEmpty(multiItem)) {
            multiItem = null;
        }
        return multiItem;
    }

    /**
     * 获取产品单位的特殊下拉框
     * @param fieldAttrEntity
     * @param jsonObject
     * @return 下拉框值对应的显示
     */
    public static Object getUnitItemList(FieldAttrEntity fieldAttrEntity, JSONObject jsonObject) {
        // 产品多单位特殊逻辑
        Object itemValue = jsonObject.get(fieldAttrEntity.getAttr());
        List<MultiUnitItemPoJo> multiItems = fieldAttrEntity.getMultiItems();
        if (CollectionsUtil.isNotEmpty(multiItems)) {
            List<ItemPoJo> itemPoJos = fieldAttrEntity.getItems();
            Map<String, ItemPoJo> itemMap = new HashMap<>(itemPoJos.size());
            Set<String> textSet = new HashSet<>();
            multiItems.forEach(item->{
                ItemPoJo itemPoJo = new ItemPoJo();
                itemPoJo.setText(item.getText());
                itemPoJo.setValue(item.getValue());
                itemMap.put(item.getValue().toString(), itemPoJo);
                textSet.add(item.getText());
            });
            if (StringUtil.isEmpty(jsonObject.getString(fieldAttrEntity.getAttr()))){
                itemValue = "";
            }else {
                ItemPoJo itemPoJo = itemMap.get(itemValue.toString());
                if (Objects.nonNull(itemPoJo)) {
                    itemValue = itemPoJo.getText();
                } else {
                    itemValue = itemValue.toString();
                }
            }
        }
        return itemValue;
    }

    /**
     * 已经删除的选项
     * @param value
     * @param fieldAttrEntity
     * @return
     */
    public static boolean isDeleteCheck(Object value, FieldAttrEntity fieldAttrEntity) {
        if (Objects.isNull(value)) {
            return true;
        }
        return isUUID(value.toString()) || (RegexUtil.isOnePositiveInt(value.toString()) && Objects.equals(fieldAttrEntity.getIsRedundant(), 0));
    }
    /**
     * 根据显示值text或者对应的value值
     * @param fieldAttrEntity
     * @param text
     * @return 下拉框text对应的code
     */
    public static Object getCustomItemValue(FieldAttrEntity fieldAttrEntity, String text) throws XbbException {

        if (fieldAttrEntity == null) {
            return null;
        }
        if (!Objects.equals(ComboTypeEnum.CUSTOM.getType(), fieldAttrEntity.getComboType())) {
            return text;
        }
        if (StringUtil.isEmpty(text)) {
            return null;
        }
        List<ItemPoJo> itemPoJos = fieldAttrEntity.getItems();

        Map<String, ItemPoJo> itemMap = new HashMap<>(itemPoJos.size());
        //其他选项
        boolean isOther = false;
        for (ItemPoJo itemPoJo : itemPoJos) {
            if (Objects.equals(1, itemPoJo.getIsOther())) {
                isOther = true;
            }
            itemMap.put(itemPoJo.getText(), itemPoJo);
        }

        boolean notChecked = isDeleteCheck(text, fieldAttrEntity);
        ItemPoJo thisItem = itemMap.get(text);
        if (thisItem == null && isOther) {
            //没有对应的，并且存在其他项，则表示此项属于其他项
           return text;
        }else if (Objects.isNull(thisItem) && notChecked) {
             // 系统字段中
            throw new XbbException(BusinessRuleErrorCodeEnum.API_ERROR_261023, BusinessRuleErrorCodeEnum.API_ERROR_261023.getMsg(), fieldAttrEntity.getAttrName());
        }else if (Objects.isNull(thisItem)) {
            throw new XbbException(BusinessRuleErrorCodeEnum.API_ERROR_261023, BusinessRuleErrorCodeEnum.API_ERROR_261023.getMsg(), fieldAttrEntity.getAttrName());
        }
        return thisItem.getValue();
    }

    /**
     * 下拉复选和复选框，根据text获取value；
     * @param fieldAttrEntity
     * @param list
     * @return
     */
    public static Object getCustomItemList(FieldAttrEntity fieldAttrEntity, List<String> list) {
        List<Object> resultList = new ArrayList<>();
        if (Objects.isNull(list)) {
            return null;
        }
        if (!Objects.equals(ComboTypeEnum.CUSTOM.getType(), fieldAttrEntity.getComboType())) {
            return list;
        }
        List<ItemPoJo> itemPoJos = fieldAttrEntity.getItems();
        itemPoJos.forEach(itemPoJo -> {
            if (list.contains(itemPoJo.getText())) {
                resultList.add(itemPoJo.getValue());
                list.remove(itemPoJo.getText());
            }
        });
        list.forEach(o -> resultList.add(o));
        return resultList;
    }



     /**
     * 解析下拉框、下拉复选、单选按钮、复选按钮
     * @param jsonObject 目标jsonObject
     * @return 返回字符串，如果是下拉框复选和复选按钮则返回：选项值1、选项值2
     */
    public static Object analysisComboReturnStr(JSONObject jsonObject,  FieldAttrEntity fieldAttrEntity){

        if (Objects.equals(fieldAttrEntity.getComboType(), ComboTypeEnum.CUSTOM.getType())) {

            return getCustom(jsonObject, fieldAttrEntity);
        } else if (Objects.equals(fieldAttrEntity.getComboType(), ComboTypeEnum.DATARELY.getType()) ||
                Objects.equals(fieldAttrEntity.getComboType(), ComboTypeEnum.LINKFORM.getType()) ||
                Objects.equals(fieldAttrEntity.getComboType(), ComboTypeEnum.BUSINESS.getType())) {
            if (jsonObject.get(fieldAttrEntity.getAttr()) instanceof ArrayList || jsonObject.get(fieldAttrEntity.getAttr()) instanceof JSONArray) {
                // 下拉框解析
                JSONArray jsonArray = jsonObject.getJSONArray(fieldAttrEntity.getAttr());
                List<ItemPoJo> itemPoJos = fieldAttrEntity.getItems();
                Map<Object, ItemPoJo> itemMap = new HashMap<>(itemPoJos.size());
                itemPoJos.forEach(item->{
                    itemMap.put(item.getValue(), item);
                });
                if (Objects.nonNull(jsonArray)) {
                    Boolean isCustom = false;
                    for(Object object: jsonArray) {
                        if (itemMap.containsKey(object)) {
                            isCustom = true;
                        }
                    }
                    if (isCustom) {
                        return getCustom(jsonObject,  fieldAttrEntity);
                    } else {
                        String value = StringUtils.join(jsonArray, SEPARATOR);
                        return value;
                    }

                }
            } else {
                return ItemUtil.getCustomItemList(fieldAttrEntity, jsonObject);
            }
        }
        return null;
    }

    /**
     * 解析下拉框、下拉复选、单选按钮、复选按钮(带颜色)
     * @param jsonObject 目标jsonObject
     * @return ItemPOJO或List<ItemPOJO>
     */
    public static Object analysisComboReturnStrWithColor(JSONObject jsonObject,  FieldAttrEntity fieldAttrEntity){
        return getCommonItem(fieldAttrEntity, jsonObject);
    }


    /**
     * 解析数组
     * @param jsonObject 数据
     * @param fieldAttrEntity 字段值
     * @return 字符串的
     */
    public static Object getCustom(JSONObject jsonObject,  FieldAttrEntity fieldAttrEntity){
        Object itemValue = ItemUtil.getCustomItemList(fieldAttrEntity, jsonObject);
        if (itemValue instanceof ArrayList || itemValue instanceof JSONArray) {
            List<String> list = JSONArray.parseArray(JSON.toJSONString(itemValue), String.class);
            itemValue = StringUtils.join(list, SEPARATOR);
        }
        return itemValue;
    }

    /**
     * 解析下拉框复选
     * @param jsonObject
     * @param fieldAttrEntity
     * @return 返回数组的下拉框
     */
    public static List<Object> analysisComboReturnList(JSONObject jsonObject,  FieldAttrEntity fieldAttrEntity) {
        if (Objects.equals(fieldAttrEntity.getComboType(), ComboTypeEnum.CUSTOM.getType())) {
            Object itemValue = ItemUtil.getCustomItemList(fieldAttrEntity, jsonObject);
            if (Objects.nonNull(itemValue) && StringUtil.isNotEmpty(itemValue.toString())) {
                List<Object> list = JSONArray.parseArray(JSON.toJSONString(itemValue), Object.class);
                return list;
            }
        } else if (Objects.equals(fieldAttrEntity.getComboType(), ComboTypeEnum.DATARELY.getType()) ||
                Objects.equals(fieldAttrEntity.getComboType(), ComboTypeEnum.LINKFORM.getType())||
                Objects.equals(fieldAttrEntity.getComboType(), ComboTypeEnum.BUSINESS.getType())) {
            JSONArray jsonArray = jsonObject.getJSONArray(fieldAttrEntity.getAttr());
            List<ItemPoJo> itemPoJos = fieldAttrEntity.getItems();
            Map<Object, ItemPoJo> itemMap = new HashMap<>(itemPoJos.size());
            itemPoJos.forEach(item->{
                itemMap.put(item.getValue(), item);
            });
            if (Objects.nonNull(jsonArray)) {
                Boolean isCustom = false;
                for(Object object: jsonArray) {
                    if (itemMap.containsKey(object)) {
                        isCustom = true;
                    }
                }
                if (isCustom) {
                    Object itemValue = ItemUtil.getCustomItemList(fieldAttrEntity, jsonObject);
                    List<Object> list = JSONArray.parseArray(JSON.toJSONString(itemValue), Object.class);
                    return list;
                } else {
                    return jsonArray;
                }

            }

        }
        return null;
    }

    public static Boolean isUUID(String uuId) {
        Matcher matcher = IS_UUID.matcher(uuId);
        return matcher.matches();
    }

    public static Object getText(Object value, FieldAttrEntity fieldAttrEntity) {
        Object text = null;
        Boolean isCombo = Objects.equals(FieldTypeEnum.COMBO.getType(), fieldAttrEntity.getFieldType())
                || Objects.equals(FieldTypeEnum.RADIO_GROUP.getType(), fieldAttrEntity.getFieldType())
                || Objects.equals(FieldTypeEnum.COMBO_CHECK.getType(), fieldAttrEntity.getFieldType())
                || Objects.equals(FieldTypeEnum.CHECKBOX_GROUP.getType(), fieldAttrEntity.getFieldType());
        if (Objects.nonNull(value) && ItemUtil.isUUID(value.toString()) && isCombo) {
            List<ItemPoJo> itemList = fieldAttrEntity.getItems();
            Map<Object, String> valueMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
            itemList.forEach(item->{
                valueMap.put(item.getValue(), item.getText());
            });
            if (valueMap.containsKey(value)) {
                text = valueMap.get(value) + "（自定义）";
            }
        } else {
            text = value;
        }
        return text;
    }

    public static String getTextByValue(Object value, FieldAttrEntity fieldAttrEntity) {
        String text = null;
        if (Objects.nonNull(value) && ItemUtil.isUUID(value.toString())) {
            List<ItemPoJo> itemList = fieldAttrEntity.getItems();
            Map<Object, String> valueMap = new HashMap<>(itemList.size());
            boolean hasOther = false;
            for (ItemPoJo item : itemList) {
                if (Objects.equals(item.getIsOther(), 1)) {
                    hasOther = true;
                }
                valueMap.put(item.getValue(), item.getText());
            }
            if (valueMap.containsKey(value)) {
                text = valueMap.get(value);
            } else if (hasOther) {
                return String.valueOf(value);
            }
        }
        return text;
    }

    public static Object getValue(Object object, FieldAttrEntity targetField) {
        if (object instanceof String) {
            // 下拉复选框，复选框组 可以输入多个文本多个值用；隔开
            String value = object.toString();
            List<String> list;
            if (value.contains(PaasConstant.CHINESE_SEMICOLON)) {
                String[] valueList = value.split(PaasConstant.CHINESE_SEMICOLON);
                list = Arrays.asList(valueList);
            } else {
                list = Arrays.asList(value);
            }
            if (Objects.equals(ComboTypeEnum.CUSTOM.getType(), targetField.getComboType())) {
                // 根据text去拿value的值
                Object result = ItemUtil.getCustomItemList(targetField, list);
                List<Object> valueList = JSON.parseArray(JSON.toJSONString(result), Object.class);
                if (CollectionsUtil.isEmpty(valueList)) {
                    // 避免不存在的值去更新数据
                    return null;
                }
                return valueList;
            }else {
                // 直接当做value存放
                return list;
            }
        } else if (object instanceof List) {
            List<ItemPoJo> itemList = JSON.parseArray(JSON.toJSONString(object), ItemPoJo.class);
            List<Object> valueList = new ArrayList<>();
            itemList.forEach(value->{
                valueList.add(value.getValue());
            });
            return valueList;
        }
        return null;
    }

    /**
     * 从表单数据ItemPoJo中解析值(兼容字符串)
     *
     * @param data
     * @param attr
     * @param defaultValue
     */
    public static String parseItemPoJoFromFormData2String(JSONObject data, String attr, String defaultValue) {
        Object o = Optional.ofNullable(data.get(attr)).orElse(defaultValue);
        if (o instanceof ItemPoJo) {
            return ((ItemPoJo) o).getText();
        } else if (o instanceof List) {
            StringBuilder str = new StringBuilder();
            for (ItemPoJo itemPoJo : (List<ItemPoJo>) o) {
                if (str.length() != 0) {
                    str.append(StringConstant.COMMA);
                }
                str.append(itemPoJo.getText());
            }
            return str.toString();
        } else {
            return o.toString();
        }
    }
}
